package com.xxx.service.impl;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxx.common.IdWorkerManage;
import com.xxx.common.dto.Page;
import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.RegexValidateUtil;
import com.xxx.dao.SysUserBasisDao;
import com.xxx.model.SysUserBasis;
import com.xxx.service.SysUserBasisService;

@Service
public class SysUserBasisServiceImpl implements SysUserBasisService {
	
	@Autowired
	private SysUserBasisDao sysUserBasisDao;
	
	@Override
	@Transactional
	public long saveOrUpdate(SysUserBasis sysUserBasis) throws Exception {
		if(sysUserBasis.getId() == null){
			sysUserBasis.setId(IdWorkerManage.getId());
			//初始密码：123456
			String password = DigestUtils.md5Hex("123456").substring(8, 24);
			sysUserBasis.setPassword(password);
			return sysUserBasisDao.save(sysUserBasis);
		}
		return sysUserBasisDao.update(sysUserBasis);
	}

	@Override
	@Transactional
	public boolean delete(long id) throws Exception{
		return sysUserBasisDao.delete(id);
	}

	@Override
	public SysUserBasis getEntity(long id) throws Exception{
		return sysUserBasisDao.getEntity(id);
	}

	@Override
	public SysUserBasis getEntityByLoginCode(String logincode) throws Exception {
		return sysUserBasisDao.getEntityByLoginCode(logincode);
	}

	@Override
	public SysUserBasis getEntityByPhone(String phone) throws Exception {
		return sysUserBasisDao.getEntityByPhone(phone);
	}
	
	@Override
	public int getCount(Map<String, Object> whereMap) {
		return sysUserBasisDao.getCount(whereMap);
	}
	
	@Override
	public List<SysUserBasis> getList(Map<String, Object> whereMap) {
		return sysUserBasisDao.getList(whereMap);
	}

	@Override
	public List<SysUserBasis> getList(Map<String, Object> whereMap, Pagination pagination) {
		return sysUserBasisDao.getList(whereMap, pagination);
	}
	
	@Override
	public Page<SysUserBasis> getPage(Map<String, Object> whereMap, Pagination pagination) {
		int count = this.getCount(whereMap);
		if(count > 0){
			List<SysUserBasis> list = this.getList(whereMap, pagination);
			return new Page<SysUserBasis>(pagination.getCurrentPage(), pagination.getPageSize(), count, list);
		}
		return new Page<SysUserBasis>(pagination.getCurrentPage(), count);
	}

	@Override
	public SysUserBasis login(String account, String pwd) throws Exception {
		SysUserBasis user = RegexValidateUtil.isPhone(account) ? this.getEntityByPhone(account) : this.getEntityByLoginCode(account);
		if(user.getId() != null && user.getStatus() == 1 && user.getPassword().equalsIgnoreCase(pwd)){
			return user;
		}
		return null;
	}

	@Override
	@Transactional
	public boolean resetPwd(long id, String pwd) throws Exception {
		return sysUserBasisDao.resetPwd(id, pwd);
	}

}
